package com.tenton.servlet;

import com.tenton.pojo.User;
import com.tenton.service.Impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/13
 * @Author: Tenton
 * @Description: 用户注册
 */
@WebServlet(name = "RegistServlet" , urlPatterns = "/registServlet")
public class RegistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        UserServiceImpl userServiceImpl = new UserServiceImpl();
        //用户名
        String userName = request.getParameter("userName");
        //密码
        String password = request.getParameter("password");
        //邮箱
        String email = request.getParameter("email");
        //省
        String province = request.getParameter("province");
        //市
        String city = request.getParameter("city");
        //爱好
        String hobby = request.getParameter("hobby");
        User user = new User(userName,password,email,province,city,hobby);
        int result = userServiceImpl.insertUser(user);
        if (result == 1){
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else {
            response.getWriter().write("注册失败，2秒后自动跳转到注册界面");
            response.setHeader("Refresh","3;URL=regist.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
